package org.jfk.config;

import javax.servlet.http.HttpServletRequest;

import org.jfk.common.ctrl.FileController;
import org.jfk.common.ctrl.SuggestController;
import org.jfk.core.MyInterceptor;
import org.jfk.core.controller.E189IdController;
import org.jfk.core.controller.EhCacheController;
import org.jfk.core.controller.IndexController;
import org.jfk.core.handler.PageSkipHandler;
import org.jfk.core.interceptor.HttpRequestLogInterceptor;
import org.jfk.core.model.AttachmentFile;
import org.jfk.core.model.HttpRequestBlackList;
import org.jfk.core.model.HttpRequestLog;
import org.jfk.sys.dictionary.Dict;
import org.jfk.sys.dictionary.DictController;
import org.jfk.sys.dictionary.DictType;
import org.jfk.sys.dictionary.DictTypeController;
import org.jfk.sys.menu.Menu;
import org.jfk.sys.menu.MenuController;
import org.jfk.sys.menu.UrlController;
import org.jfk.sys.org.Org;
import org.jfk.sys.org.OrgController;
import org.jfk.sys.role.Role;
import org.jfk.sys.role.RoleController;
import org.jfk.sys.role.RoleUrl;
import org.jfk.sys.security.Urls;
import org.jfk.sys.test.TestController;
import org.jfk.sys.user.User;
import org.jfk.sys.user.UserController;
import org.jfk.sys.user.UserRole;
import org.jfk.tbox.controller.TboxDeviceController;
import org.jfk.tbox.controller.TboxMsgLogController;
import org.jfk.tbox.model.TboxDevice;
import org.jfk.tbox.model.TboxMsgLog;
import org.jfk.tbox.plugin.TboxTcpServerPlugin;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByMethods;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

public class SystemConfig extends JFinalConfig {
	
	public final static String INIT_PASS = "000000";

	@Override
	public void configConstant(Constants me) {
		
		loadPropertyFile("system_config.properties");				//加载数据库配置文件
		PropKit.use("system_config.properties");
		
		me.setDevMode(PropKit.getBoolean("sys.devMode"));

		me.setViewType(ViewType.JSP); 							// 设置视图类型为Jsp，否则默认为FreeMarker
		me.setViewExtension(".jsp");                            //升级到jfinal 3.1之后默认是html，否则要专门调用此方法指定
		me.setError404View("/error/400.jsp");
		me.setError500View("/error/500.jsp");
		
	}

	@Override
	public void configRoute(Routes me) {

		me.add("/",IndexController.class);

		me.add("/common/ctrl/suggest", SuggestController.class);
		me.add("/common/ctrl/file",FileController.class);

		me.add("/sys/dictionary/dictType", DictTypeController.class);
		me.add("/sys/dictionary/dict", DictController.class);
		me.add("/sys/user", UserController.class);
		me.add("/sys/org", OrgController.class);
		me.add("/sys/role", RoleController.class);
		me.add("/sys/menu", MenuController.class);
		me.add("/sys/url", UrlController.class);
		
		me.add("/sys/ehcache", EhCacheController.class);
		

		me.add("/test", TestController.class);

		me.add("/tbox/tboxDevice", TboxDeviceController.class);
		me.add("/tbox/tboxMsgLog", TboxMsgLogController.class);
		
		me.add("/e189/id", E189IdController.class);
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
//		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
//		me.add(c3p0Plugin);
//		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		


		/**配置druid数据连接池插件**/
		DruidPlugin dp=new DruidPlugin(getProperty("db.jdbcUrl"),getProperty("db.user"),getProperty("db.password").trim());
		dp.setValidationQuery(getProperty("db.validSql"));
		/**配置druid监控**/
		dp.addFilter(new StatFilter());
		WallFilter wall=new WallFilter();
		wall.setDbType(getProperty("db.dbType"));
		dp.addFilter(wall);
		me.add(dp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		
		me.add(arp);
		
		
		
		me.add(new EhCachePlugin());
		
		// 配置ActiveRecord插件
		me.add(arp);
		arp.addMapping("jfk_dict_type", DictType.class);	
		arp.addMapping("jfk_dict", Dict.class);	
		arp.addMapping("jfk_user", User.class);	
		arp.addMapping("jfk_menu", Menu.class);	
		arp.addMapping("jfk_urls", Urls.class);	
		arp.addMapping("jfk_org" ,  Org.class);	
		arp.addMapping("jfk_role" ,  Role.class);	
		arp.addMapping("jfk_role_urls" ,  RoleUrl.class);	
		arp.addMapping("jfk_user_role" ,  UserRole.class);	

		arp.addMapping("jfk_http_request_black_list" ,  HttpRequestBlackList.class);	
		arp.addMapping("jfk_http_request_log" ,  HttpRequestLog.class);	

		arp.addMapping("jfk_attachment_file" ,  AttachmentFile.class);	

		arp.addMapping("tbox_device" ,  TboxDevice.class);	
		arp.addMapping("tbox_msglog" ,  TboxMsgLog.class);	
		
		
		TboxTcpServerPlugin serverPlugin = new TboxTcpServerPlugin();
		me.add(serverPlugin);
		
		
		arp.setShowSql(true);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new MyInterceptor());
		//me.add(new ExceptionInterceptor());
		//me.add(new RefererInterceptor());
		me.add(new HttpRequestLogInterceptor());
		me.add(new TxByMethods("save","del","delAll"));
		//me.add(new TxByActionKeyRegex("[\\d|\\D]*"));
		
	}

	@Override
	public void configHandler(Handlers me) {
		
		//添加druid控制台，并限定只有admin用户可以查看
		DruidStatViewHandler dvh=new DruidStatViewHandler("druid",new IDruidStatViewAuth(){
		    public boolean isPermitted(HttpServletRequest request) {
		        User user=(User) request.getSession().getAttribute("currUser");
		        if(user==null){
		            return false;
		        }
		        return "admin".equals(user.getStr("loginName"));
		    }
		});
		me.add(dvh);
		
		//禁止直接访问jsp，html等页面
		me.add(new PageSkipHandler());
		
	}

	@Override
	public void configEngine(Engine arg0) {
		// TODO Auto-generated method stub
		
	}

}
